var express = require('express');
var router = express.Router();
var querystring = require('querystring');
var url = require('url');
var http = require('http');
var https = require('https');
var util = require('util');

//解决express跨域问题
router.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  next();
});

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});


// router.get('/sj', function(req, res) {

//     var urlstr = 'http://i.waimai.meituan.com/ajax/v6/poi/filter'
//     var urlData = url.parse(urlstr);
//     //POST 内容
// var bodyQueryStr = {
//     lat: 22.543099,
//     lng: 114.057868,
//     _token: 'eJx9jVFrgzAUhf/LfdjLJCapUSOU4eoeLNOxJvVl7CFzYsMaWzTbrGP/vSkIZS+7XPjOPZzL+YE+f4eEYDfcAzs4zTBhcRDSmAfMg/qPxwmNPHjrqwySl0XEvDCMXi/Gxt1X46po4PaSyF0AdtYeE9/X6FtpozQyjbafqkP1wfi7g2nu9souKUUsWGDOb/ZduyQkQJhFcRiD9+9/re0JXJORrsnxY6aaaWcOuu0ggWY9SqHHXD7ckrKSj0KcxtKhmLZj2a9wVXytRVSTMpObYkqJnVbTNn066tovBDZ2yKrntL13HqPwewZUhF7n'
// };

// var contentStr = querystring.stringify(bodyQueryStr);
// var contentLen = Buffer.byteLength(contentStr, 'utf8');
// console.log(util.format('post data: %s, with length: %d', contentStr, contentLen));
// var httpModule = urlstr.indexOf('https') === 0 ? https : http;
// var urlData = url.parse(urlstr);

// //HTTP请求选项
// var opt = {
//     hostname: urlData.hostname,
//     path: urlData.path,
//     method: 'POST',
//     headers: {
//         'Content-Type': 'application/x-www-form-urlencoded',
//         'Content-Length': contentLen
//     }
// };

// //处理事件回调
// var req = httpModule.request(opt, function(httpRes) {
//     var buffers = [];
//     httpRes.on('data', function(chunk) {
//         buffers.push(chunk);
//     });

//     httpRes.on('end', function(chunk) {
//         var wholeData = Buffer.concat(buffers);
//         var dataStr = wholeData.toString('utf8');
//         console.log('content ' + wholeData);
//         res.send(dataStr)
//     });
// }).on('error', function(err) {
//     console.log('error ' + err);
// });;

// //写入数据，完成发送
// req.write(contentStr);
// req.end();
// });

router.get('/nearby', function (req, res) {  //附近美食
  var lat = req.query.latitude
  var lot = req.query.longitude

  https.get('https://mainsite-restapi.ele.me/shopping/v2/entries?latitude=' + lat + '&longitude=' + lot
    + '&templates[]=main_template', function (response) {
      var data = ''
      response.on('data', function (chunk) {
        data += chunk
      })

      response.on('end', function () {
        res.send(data)
      })
    })

});

router.get('/seller', function (req, res) {  //商家
  var lat = req.query.latitude
  var lot = req.query.longitude
  var limit = req.query.limit

  https.get('https://mainsite-restapi.ele.me/shopping/restaurants?latitude=' + lat + '&longitude=' + lot
    + '&offset=20&limit=' + limit + '&extras[]=activities&terminal=h5', function (response) {
      var data = ''
      response.on('data', function (chunk) {
        data += chunk
      })

      response.on('end', function () {
        res.send(data)
      })
    })
});

router.get('/hot_search', function (req, res) {  //热搜
  var lat = req.query.latitude
  var lot = req.query.longitude

  https.get('https://mainsite-restapi.ele.me/shopping/v3/hot_search_words?latitude=' + lat + '&longitude=' + lot + '&geohash=ws105rz9smwm', function (response) {
    var data = ''
    response.on('data', function (chunk) {
      data += chunk
    })

    response.on('end', function () {
      res.send(data)
    })
  })
});

module.exports = router;
